﻿// ==========================================================================
// CustomConnection.cs
// Silverlight Shapes Library
// ==========================================================================
// Copyright (c) Silverlight Shapes Development Group
// All rights reserved.
// ==========================================================================

using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interactivity;
using SD.Shapes.Common.Connections;
using SD.Shapes.Common.Interactivity;
using SD.Utils.Windows.Interactivity;

namespace SD.Shapes.Samples.CustomConnection
{
    /// <summary>
    /// This is a custom connection that renders a text and a multiplicity at the source and the sink
    /// of the connection. The text editor adorner is not added to the control, instead a custom dialog is used.
    /// </summary>
    public class MyConnection : Connection
    {
        #region Dependency Properties

        /// <summary>
        /// Defines the <see cref="SourceText"/> dependency property.
        /// </summary>
        public static readonly DependencyProperty SourceTextProperty =
            DependencyProperty.Register("SourceText", typeof(string), typeof(MyConnection), new PropertyMetadata(string.Empty));
        /// <summary>
        /// Gets or sets the text that is displayed at the source of the connection.
        /// </summary>
        /// <value>The text that is displayed at the source of the connection.</value>
        public string SourceText
        {
            get { return (string)GetValue(SourceTextProperty); }
            set { SetValue(SourceTextProperty, value); }
        }

        /// <summary>
        /// Defines the <see cref="SourceMultiplicity"/> dependency property.
        /// </summary>
        public static readonly DependencyProperty SourceMultiplicityProperty =
            DependencyProperty.Register("SourceMultiplicity", typeof(string), typeof(MyConnection), new PropertyMetadata("*"));
        /// <summary>
        /// Gets or sets the multiplicity that is displayed at the source of the connection.
        /// </summary>
        /// <value>The multiplicity that is displayed at the source of the connection.</value>
        public string SourceMultiplicity
        {
            get { return (string)GetValue(SourceMultiplicityProperty); }
            set { SetValue(SourceMultiplicityProperty, value); }
        }

        /// <summary>
        /// Defines the <see cref="SinkText"/> dependency property.
        /// </summary>
        public static readonly DependencyProperty SinkTextProperty =
            DependencyProperty.Register("SinkText", typeof(string), typeof(MyConnection), new PropertyMetadata(string.Empty));
        /// <summary>
        /// Gets or sets the text that is displayed at the source of the connection.
        /// </summary>
        /// <value>The text that is displayed at the source of the connection.</value>
        public string SinkText
        {
            get { return (string)GetValue(SinkTextProperty); }
            set { SetValue(SinkTextProperty, value); }
        }

        /// <summary>
        /// Defines the <see cref="SinkMultiplicity"/> dependency property.
        /// </summary>
        public static readonly DependencyProperty SinkMultiplicityProperty =
            DependencyProperty.Register("SinkMultiplicity", typeof(string), typeof(MyConnection), new PropertyMetadata("*"));
        /// <summary>
        /// Gets or sets the multiplicity that is displayed at the source of the connection.
        /// </summary>
        /// <value>The multiplicity that is displayed at the source of the connection.</value>
        public string SinkMultiplicity
        {
            get { return (string)GetValue(SinkMultiplicityProperty); }
            set { SetValue(SinkMultiplicityProperty, value); }
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="MyConnection"/> class.
        /// </summary>
        public MyConnection()
        {
            DefaultStyleKey = typeof(MyConnection);

            BehaviorCollection behaviors = Interactions.GetBehaviors(this);
            behaviors.Add(new ShapeSelectionBehavior());
            behaviors.Add(new ConnectionMovementBehavior());

            AdornerCollection adorners = Interactions.GetAdorners(this);
            adorners.Add(new ConnectionPointAdorner());
            adorners.Add(new ConnectionSegmentAdorner());
        }

        #endregion

        #region Methods

        /// <summary>
        /// Raises the <see cref="E:DoubleClick"/> event.
        /// </summary>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected override void OnDoubleClick(MouseButtonEventArgs e)
        {
            MyConnectionEditor editor = new MyConnectionEditor();
            editor.DataContext = this;
            editor.Show();

            base.OnDoubleClick(e);
        }
        #endregion
    }
}
